package com.ankicoo.practice.daily;
import java.util.Objects;

/**
 * Description:
 *
 * @author : QCYANG
 * @date : 2022/11/7 17:54
 */
public class LC1106 {
    private String expr ;
    public boolean parseBoolExpr(String expression) {
        this.expr = expression;
        char first = this.expr.charAt(0);
        Boolean result = null;
        while(expression.charAt(0)!=')'){
            result = parseExpr();
        }
        return true;
    }

    private boolean parseExpr(){
        char first = this.expr.charAt(0);
        this.expr = this.expr.substring(2);
        Boolean result = null;

        while(this.expr.charAt(0)!=')'){
            char subFirst = this.expr.charAt(0);
            if (subFirst=='!'||subFirst=='|'||subFirst=='&'){
                result = parseExpr();
                continue;
            }
            if (subFirst=='f'||subFirst=='t'){
                result = parseBool(result,subFirst,first);
            }
            this.expr = this.expr.substring(1);
        }
        return result;
    }


    private boolean parseBool(Boolean bool1,char bool2Expr,char choice){
        boolean bool2 = (bool2Expr == 't');
        switch (choice){
            case '!' : return !bool2;
            case '|' : return Objects.isNull(bool1)?bool2:(bool1||bool2);
            case '&' : return Objects.isNull(bool1)?bool2:(bool1&&bool2);
            default: throw new RuntimeException("解析异常");
        }
    }
}
